package org.bot.server.flow.controller;

import com.alibaba.fastjson.JSONObject;
import org.bot.server.config.LoginCache;
import org.bot.server.flow.entity.BotFlow;
import org.bot.server.flow.service.BotFlowService;
import org.bot.server.utils.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @author 沧月
 * @create 0021 2023/9/21 14:01
 * @description <description class purpose>
 */
@RestController
@RequestMapping("/bot-flow")
public class BotFlowController extends BaseController {

    @Autowired
    BotFlowService botFlowService;

    @RequestMapping("/list")
    public JSONObject list(String name,Integer clientId, Integer page, Integer size) {
        List<Map<String,Object>> list = botFlowService.list(name, clientId,page, size);
        Integer count = botFlowService.listCount(name);
        JSONObject data = new JSONObject().fluentPut("data", list).fluentPut("count", count);
        return sendData(data);
    }


    @RequestMapping("/save")
    public JSONObject save(BotFlow botFlow) {
        if (botFlowService.save(botFlow)) {
            return sendOK();
        } else {
            return sendError("保存失败");
        }
    }


    @RequestMapping("/delete")
    public JSONObject delete(Integer id) {
        if (botFlowService.delete(id)) {
            return sendOK();
        } else {
            return sendError("删除失败");
        }
    }


    @RequestMapping("/edit")
    public JSONObject edit(BotFlow botFlow) {
        if (botFlowService.edit(botFlow)) {
            return sendOK();
        } else {
            return sendError("保存失败");
        }
    }


    @RequestMapping("/updateFlowInfo")
    public JSONObject updateFlowInfo(Integer id, String x6JSON, String botJSON) {
        if (botFlowService.updateFlowInfo(id, x6JSON, botJSON)) {
            return sendOK();
        } else {
            return sendError("保存失败");
        }
    }


    @RequestMapping("/getClientList")
    public JSONObject getClientList() {
        return sendData(botFlowService.getClientList());
    }


    @RequestMapping("/getFlowInfo")
    public JSONObject getFlowInfo(Integer id){
        if(id == null){
            return sendError("加载流程ID错误");
        }
        return sendData(botFlowService.getFlowInfo(id));
    }

    @RequestMapping("/executeFlow")
    public JSONObject executeFlow(Integer id){
        if(id == null){
            return sendError("加载流程ID错误");
        }
        if(botFlowService.executeFlow(id)){
            return sendOK();
        } else {
            return sendError("执行失败");
        }
    }

    @RequestMapping("/disable")
    public JSONObject disable(String id, Integer status) {
        if (id == null || status == null) {
            return sendError("参数错误");
        }
        if (botFlowService.disable(id, status)) {
            return sendOK();
        } else {
            return sendError("禁用失败");
        }
    }

    @RequestMapping("/downloadFlow")
    public void downloadFlow(HttpServletResponse response,String ids,String token){
        if(!LoginCache.isLoginValid(token)){
            return;
        }
        botFlowService.downloadFlow(response, ids);
    }


    @RequestMapping("/uploadFlow")
    public JSONObject uploadFlow(@RequestParam("file") MultipartFile file) {
        if (botFlowService.uploadFlow(file)) {
            return sendOK();
        } else {
            return sendError("上传流程包失败");
        }
    }
}
